package GUI;

import 学生.Student;
import 数据库和网络.myDatebase;
import 试题和试卷.paper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

public class chooseReviseStudentItem {

    String[] papername;
    int[] papertime;
    Student currentstudent;
    JButton reviseStudent;

    static chooseReviseStudentItem chooseReviseStudentItem;
    public  chooseReviseStudentItem(int index,Student curr ){
        currentstudent =curr;
        reviseStudent = new JButton(curr.getName());
        reviseStudent.addActionListener(new stuChooseListener());
        int row = index/7+1;
        int col = index%7;
        reviseStudent.setBounds(100*col-50,50*row+50,80,30);
        reviseStudent.setBackground(Color.lightGray);
    }


    public class stuChooseListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            myDatebase queryDateBase=new myDatebase("information_schema");
            myDatebase paperDateBase=new myDatebase("papercollection");
            String tableName="";
            int paperNumber=0;

            ArrayList<paper> papers;

            try {
                String sql="select TABLE_NAME from information_schema.TABLES where information_schema.TABLES.TABLE_NAME='"+currentstudent.getMyClass()+"' ";
                queryDateBase.statement.execute(sql);
                queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql);
                while (queryDateBase.resultSet.next()){
                    tableName=queryDateBase.resultSet.getString(1);
                    System.out.println(tableName);
                }/*上面是寻找到currentsutdent所在的班级*/

                String sql1="select TABLE_ROWS from TABLES where TABLE_NAME='"+tableName+"'";
                queryDateBase.statement.execute(sql1);
                queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql1);
                while(queryDateBase.resultSet.next()){
                    paperNumber=queryDateBase.resultSet.getInt(1);
                }/*上面是从TABLE表中确定试卷数，以上是为了通过currentStudent的班级信息连接上数据库自带的存储信息的TABLES表，从而找到
        对应班级的试卷数据库以及对应的试卷库信息*/

                String sql2=String.format("select * from papercollection.%s",tableName);
                System.out.println(paperNumber);
                papername=new String[paperNumber];
                papertime=new int[paperNumber];
                paperDateBase.statement.execute(sql2);
                paperDateBase.resultSet=paperDateBase.statement.executeQuery(sql2);
                int index=0;
                while(paperDateBase.resultSet.next()){
                    papertime[index]=paperDateBase.resultSet.getInt(2);
                    System.out.println(papertime[index]);
                    papername[index]=paperDateBase.resultSet.getString(1);
                    System.out.println(papername[index]);
                    index++;
                /*这里是找到对应的班级数据库，从中找到paperTitle输出到String[]里，也就是papername，
                这样就可以再根据paper类中的方法把这些paper的数据加载到一个arraylist：papers中*/
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                queryDateBase.close();
                paperDateBase.close();
            }

            papers=new ArrayList<>(paperNumber);
            for(int i=0;i<paperNumber;i++){
                papers.add(new paper());
            }


            for(int i=0;i<paperNumber;i++){
                papers.get(i).getQuestions(tableName,papername[i]);
                papers.get(i).setName(papername[i]);
                papers.get(i).setTime(papertime[i]);
            }
            chooseStudentWindow.chooseStudent.dispose();
            revisePaperChooseWindow.revisePaperChoose =new revisePaperChooseWindow(currentstudent);
        }
    }


    public class reviselistener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    }
}
